{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ff7a692-9472-4360-a326-897003bec04f",
   "metadata": {},
   "source": [
    "# Paleopiezometry analysis template\n",
    "\n",
    "> **INFO**  \n",
    "> This is the template for the paleopiezometry module. The specific documentation can be found at the following link:  \n",
    "> https://github.com/marcoalopez/GrainSizeTools/wiki/4.-Using-the-paleopiezometry-module \n",
    ">\n",
    "> The template shows typical examples of paleopizometrys and different strategies for presenting them. Modify, delete and add as necessary to create your own analysis procedure.\n",
    ">\n",
    "> If you find any error in this template, please report them at https://github.com/marcoalopez/GrainSizeTools/issues  \n",
    "> If you have any questions or suggestions open a discussion at https://github.com/marcoalopez/GrainSizeTools/discussions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "957dcb9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "013a329a-5942-42e9-9131-ea82de57766d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===================================================\n",
      "Welcome to the GrainSizetool piezometers module\n",
      "===================================================\n",
      "Piezometric database v2024.06.06 loaded.\n",
      "\n",
      "To get or display piezometric properties use:\n",
      ">>> database.<mineral>.<piezometer>\n",
      "\n",
      "Available piezometric relationships:\n",
      "quartz:\n",
      "   Stipp_Tullis\n",
      "   Stipp_Tullis_BLG\n",
      "   Holyoke\n",
      "   Holyoke_BLG\n",
      "   Cross\n",
      "   Cross_HR\n",
      "   Shimizu\n",
      "   Twiss\n",
      "olivine:\n",
      "   VanderWal_wet\n",
      "   Jung_Karato\n",
      "calcite:\n",
      "   Rutter_SGR\n",
      "   Rutter_GBM\n",
      "   Barnhoorn\n",
      "   Platt_Bresser\n",
      "   Valcke\n",
      "feldspar:\n",
      "   Post_Tullis_BLG\n",
      "===================================================\n"
     ]
    }
   ],
   "source": [
    "# Load the script. Ensure the notebook is in the same folder as the\n",
    "# GrainSizeTools.py file, if not specify the full path to the file.\n",
    "# e.g. %run filepath...\\GrainSizeTools_script.py\n",
    "%run piezometers.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "379a0a94",
   "metadata": {},
   "source": [
    "## The database\n",
    "\n",
    "Examples of how to access the database and display piezometric data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a20ec96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year: 1993\n",
      "reference: https://doi.org/10.1029/93GL01382\n",
      "B: 1355.4\n",
      "m: 0.75\n",
      "warn: Please ensure that you have entered the apparent grain size as the arithmetic mean in linear scale.\n",
      "linear_intercepts: True\n",
      "correction_factor: 1.5\n",
      "notes: The Van der Wal (1993) piezometer was calibrated using the linear intercept (LI) grain size\n",
      "multiplied by 1.5 (correction factor). ECDs without stereological correction will be converted\n",
      "to LIs using the empirical equation of De Hoff and Rhines (1968) LI = (1.5 / sqrt(4/pi)) * ECD\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# display the properties of the piezometric relation to use\n",
    "summary(database.olivine.VanderWal_wet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e60e3a8c-0e26-4d0e-a3c6-0feb72ff7ffc",
   "metadata": {},
   "source": [
    "## Estimate a differential stress\n",
    "\n",
    "The GrainSizeTools script includes a method to estimate the differential stress based on the average recrystallised grain size called ``calc_diffstress()``. This function requires\n",
    "\n",
    "- Define the piezometric relation to be used.\n",
    "- Enter the (apparent) average grain size as **equivalent circle diameter** in microns using a proper average and **without any stereological correction** (the method will address this if necessary).\n",
    "- Specify the type of stress, either uniaxial compression/strain or plane strain, for correct stress correction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "76712a37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# uncomment line below to get help on how to use calc_diffstress\n",
    "#calc_diffstress?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c2466fa2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculated differential stress = 31.06 MPa\n",
      "\n",
      "INFO:\n",
      "Please ensure that you have entered the apparent grain size as the arithmetic mean in linear scale.\n",
      "The diameters have been converted to linear intercepts using the de Hoff and Rhines (1968) correction.\n",
      "The differential stress was corrected for plane stress using the methodology outlined in Paterson and Olgaard (2000).\n"
     ]
    }
   ],
   "source": [
    "calc_diffstress(database.olivine.VanderWal_wet, grain_size=140, correction=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35d51867-30b3-4ad4-a2b6-b31972344922",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Using an array of grain sizes grain size averages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8427ed68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Differential stresses in MPa\n",
      "\n",
      "INFO:\n",
      "Please ensure that you have entered the apparent grain size as the arithmetic mean in linear scale.\n",
      "The diameters have been converted to linear intercepts using the de Hoff and Rhines (1968) correction.\n"
     ]
    }
   ],
   "source": [
    "# define a set of average grain size values\n",
    "ameans = np.array([112.23, 113.71, 112.76, 111.73, 112.69, 110.67])\n",
    "\n",
    "# estimate the differential stress and store the results\n",
    "estimates = calc_diffstress(database.olivine.VanderWal_wet, grain_size=ameans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "46fe8ecd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([31.75, 31.44, 31.64, 31.86, 31.65, 32.09])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# display the results\n",
    "estimates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5587b230",
   "metadata": {},
   "source": [
    "### Estimate a confidence interval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8b235749-c4f5-467b-bbe0-0ba5c2b82eae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "module averages imported\n"
     ]
    }
   ],
   "source": [
    "from averages import conf_interval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "045f32ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      "Mean = 31.74 ± 0.23\n",
      "Confidence set at 95.0 %\n",
      "Max / min = 31.97 / 31.51\n",
      "Coefficient of variation = ±0.7 %\n"
     ]
    }
   ],
   "source": [
    "mean, confi, limits = conf_interval(estimates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e5f174dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Notebook last run in 2024-06-26 using:\n",
      "Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:16:22) [MSC v.1916 64 bit (AMD64)]\n",
      "Numpy 1.26.4\n",
      "Pandas 2.2.2\n",
      "Database metadata:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'author': 'Marco A. Lopez-Sanchez',\n",
       " 'contact_email': 'marcoalopez [to be found at] outlook [dot] com',\n",
       " 'description': 'Piezometric database from GrainSizeTools script',\n",
       " 'created_at': datetime.date(2024, 6, 6),\n",
       " 'license': 'Open Database License (ODbL) - version 1.0',\n",
       " 'usage_terms': 'https://opendatacommons.org/licenses/odbl/summary/',\n",
       " 'contributors': None}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# annotate the date you executed the notebook and the Python version \n",
    "import sys\n",
    "from datetime import date    \n",
    "today = date.today().isoformat()\n",
    "\n",
    "print(f'Notebook last run in {today} using:')\n",
    "print('Python', sys.version)\n",
    "print('Numpy', np.__version__)\n",
    "print('Pandas', pd.__version__)\n",
    "print('Database metadata:')\n",
    "metadata"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
